<?php
/**
 */
class CommentsTable extends Doctrine_Table
{
	public function getComments($trigger_id,$currentPage,$resultsPerPage)
	{
		//echo $user_id;exit;
		$query = new Doctrine_Query();
		$query->select('c.*,u.first_name as first_name,d.photo as photo,t.owner_id as owner_id')
		       ->from('Comments c')
			   ->leftJoin('c.Users u ON u.user_id=c.user_id')
			   ->leftJoin('c.TriggersRelations t ON t.trigger_id=c.trigger_id')
			   ->leftJoin('u.UserDetails d ON u.user_id=d.user_id')
			   ->where("c.trigger_id='".$trigger_id."'")
			   ->andWhere("c.status='Active'");	
			   //->groupBy('t.owner_id')
			   //->orderBy("r.trigger_id desc");
			   //echo $query;exit;
			   require_once('../database/models/Pager.php');
				$pager = new Pager($currentPage,$resultsPerPage);
				//echo $query->getSql();
				$result = $pager->pagination($query);
				//echo '<pre>';print_r($result);exit;
				return $result;
	        
	}
	public function getAllTriggerIds()
	{
		//echo $user_id;exit;
		$query = new Doctrine_Query();
		$query->select('c.*')
		       ->from('Comments c')
			   ->where("send_mail!='1'");
		$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
		return $result;
	        
	}
	public function addComment($trigger_id,$user_id,$comment)
	{
		$Comments = new Comments();
		$Comments ->trigger_id = $trigger_id;
		$Comments ->user_id = $user_id;
		$Comments ->comment = $comment;
		$Comments ->added_date = date('Y-m-d H:i:s');
		$Comments ->status = "Active";
		
        //echo '<pre>';print_r($Expressions);exit;
		$Comments ->save();
		return $Comments->id;
	}
	public function deletecomment($comment_id)
	{
		 $query = new Doctrine_Query();
		$query->update('Comments c')
			  ->set('c.status','"Inactive"')
			  ->where("c.id='".$comment_id."'");
		  //echo $query;exit;
		return $query->execute();
	}
	public function updateTriggerSendMailStatus($relation_id)
	{
		$query = new Doctrine_Query();
		$query->update('Comments e')
			  ->set('e.send_mail',"'1'")
			  ->where("e.id='".$comment_id."'");
			   //echo $query;exit;
		$result=$query->execute();
	return $result;		
	}
}